/*
 * thermometerdevice.h
 *
 *   Created on: 6.02.2011
 *       Author: Mike Shatohin (brunql)
 *
 *      Project: USBtinyThermometer
 *
 *  Copyright (c) 2011 Mike Shatohin, mikeshatohin [at] gmail.com
 *
 *  USBtinyThermometer is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  USBtinyThermometer is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <QString>

#include <usb.h>
#include "opendevice.h"

#include "../Firmware/usbtiny.h"    /* VID, PID */
#include "../Firmware/commands.h"



// Size of the buffer using in usb data transfers
#define BUFF_SIZE           8
// Buffer indexes
#define BUFF_CMD_INDEX      0
#define BUFF_LAST_RESULT    6
#define BUFF_SENSORS_NUMBER_INDEX 7

#define DELAY_BETWEEN_USB_REQUESTS_us  100*1000


#ifndef THERMOMETERDEVICE_H
#define THERMOMETERDEVICE_H

class ThermometerDevice
{

public:
    ThermometerDevice(int verboseLevel);

    int findSensors();
    void startMeasurement();
    double getThemperature(int sensorIndex, bool roundFirstDec);
    QString getSensorId(int sensorIndex);
    void close();

private:
    void usbIn();  // Read buffer
    void usbOut(); // Write buffer

    // Debug output
    void outFail(const int usb_control_msg_res, const char buff[BUFF_SIZE], const char * usb_error_str);
    void out(const char *str);
    void outBuffer();

private:
    usb_dev_handle *usbDevHandle;

    char buffer[BUFF_SIZE];

    int verboseLevel;
};

#endif // THERMOMETERDEVICE_H
